const Store = require('electron-store')
const uuidv4 = require('uuid/v4')
const path = require('path')

class DataStore extends Store {
  constructor(settings) {
    super(settings)
    this.tracks = this.get('tarcks') || []
  }

  saveTracks() {
    this.set('tracks', this.tracks)
    return this
  }

  getTracks() {
    return this.get('tracks') || []
  }

  addTracks(tracks) {
    const tracksWithProps = tracks
      .map(track => {
        return {
          id: uuidv4(),
          path: track,
          fileName: path.basename(track)
        }
      })
      .filter(track => {
        console.log('this', this.getTracks())
        const currentTracksPath = this.getTracks().map(track => track.path)
        return currentTracksPath.indexOf(track.path) < 0
      })

    this.tracks = [...this.tracks, ...tracksWithProps]
    this.saveTracks()
    return this
  }

  deleteTracks(deleteId) {
    this.tracks = this.tracks.filter(item => item.id !== deleteId)
    this.saveTracks()
    return this
  }
}

module.exports = DataStore
